package com.tecode.controller.upload;

import com.alibaba.fastjson.JSONObject;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.tecode.bean.Code;
import com.tecode.bean.Music;
import com.tecode.controller.frontend.fastdfs.UploadController;
import com.tecode.service.MusicService;
import com.tecode.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.io.File;
import java.util.*;


@Controller
public class MusicUpload {
    @Autowired
    private MusicService musicService;
    @Autowired
    private UploadController uploadController;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MusicService service;


    @RequestMapping("/uploadlogin")
    public String uploadlogin(){
        System.out.println("uploadlogin");
        return "uploadlogin";
    }

//
//    @RequestMapping("/musicUpload")
//    public String toMusicUpLoad(Map<String,Object>map){
//        List<Code> musicCode= musicService.getMusicCodeByCodeId(4);
//        System.out.println(musicCode);
//        map.put("musicCode",musicCode);
//        return "upload";
//    }

    @RequestMapping("/upload")
    public String upload(@Valid Music music, BindingResult bindingResult, @RequestParam("img") MultipartFile imgFile, @RequestParam("address")MultipartFile addressFile,Map<String,Object>map){
        String img = (String) uploadController.upload(imgFile, null).get("fileName");
        String address = (String) uploadController.upload(addressFile, null).get("fileName");
        music.setImg(img);
        music.setAddress(address);
        Date date=new Date();
        music.setCreatetime(date);
        System.out.println(music);
        int i = musicService.savaMusic(music);
        int row=0;
        if (i>0){
            row=1;
        }else {
            row=2;
        }
        List<Code> musicCode= musicService.getMusicCodeByCodeId(4);
        System.out.println(musicCode);
        map.put("musicCode",musicCode);
        map.put("row",row);
        map.put("userId",music.getUserid());

         return "upload";
    }

    /**
     * 手机号验证码登录
     * @param phone
     * @param ver
     * @param map
     * @return
     */
    @RequestMapping("/musicUpload")
    public Object musicUpload(@RequestParam("phone") String phone,@RequestParam("ver") String ver,Map<String ,Object> map){
        System.out.println("musicUpload");
        String sessionId = "";
        Date now = new Date();
        Map user = userService.getUserByPhone(phone);
        System.out.println(user);
        if (user!=null){
            if(StringUtils.isNotEmpty(ver)&&ver.equals(user.get("ver"))){
                Date ver_deadline = (Date) user.get("ver_deadline");
                if (now.after(ver_deadline)){
                    map.put("error","验证码已过期");
                }else{
                    ValueOperations valueOperations = redisTemplate.opsForValue();
                    sessionId = UUID.randomUUID().toString();
                    valueOperations.set("sessionId",sessionId);
                    map.put("sessionId",sessionId);
                    map.put("userId",user.get("userId"));
                    List<Code> musicCode= musicService.getMusicCodeByCodeId(4);
                    System.out.println(musicCode);
                    map.put("musicCode",musicCode);
                    map.put("row",0);
                    return "upload";
                }
            }else{
                map.put("error","验证码错误！");
            }

        }else {
            map.put("error","手机号未绑定！");
        }
        return "uploadlogin";
    }

}
